
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Developer API · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Devdocs.html" />
    
    
    <link rel="prev" href="../tutorials/dashboard/Dashboard.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    What is ThingsJS?
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../about/Architecture.html">
            
                <a href="../about/Architecture.html">
            
                    
                    System Architecture
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../about/Terminology.html">
            
                <a href="../about/Terminology.html">
            
                    
                    Terminology
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="CLI.html">
            
                <a href="CLI.html">
            
                    
                    CLI Commands
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="../Publications.html">
            
                <a href="../Publications.html">
            
                    
                    Publications
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../GettingStarted.html">
            
                <a href="../GettingStarted.html">
            
                    
                    Getting Started
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../tutorials/VirtualMachine.html">
            
                <a href="../tutorials/VirtualMachine.html">
            
                    
                    ThingsJS VM Image
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../tutorials/Setup-PocketBeagle.html">
            
                <a href="../tutorials/Setup-PocketBeagle.html">
            
                    
                    Pocket Beagle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../tutorials/Setup-BeagleboneBlack.html">
            
                <a href="../tutorials/Setup-BeagleboneBlack.html">
            
                    
                    Beaglebone Black
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../samples/">
            
                <a href="../samples/">
            
                    
                    Sample Components
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../samples/RIoTBenchmarks.html">
            
                <a href="../samples/RIoTBenchmarks.html">
            
                    
                    Sample Benchmarks
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../tutorials/">
            
                <a href="../tutorials/">
            
                    
                    Tutorials
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../tutorials/FirstApplication.html">
            
                <a href="../tutorials/FirstApplication.html">
            
                    
                    Writing your First App
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../tutorials/Migration.html">
            
                <a href="../tutorials/Migration.html">
            
                    
                    Live-Migration
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../tutorials/dashboard/Dashboard.html">
            
                <a href="../tutorials/dashboard/Dashboard.html">
            
                    
                    Using the Dashboard
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="1.4" data-path="./">
            
                <a href="./">
            
                    
                    Developer API
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="Devdocs.html">
            
                <a href="Devdocs.html">
            
                    
                    Developer Documentation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="Testing.html">
            
                <a href="Testing.html">
            
                    
                    Running Unit Tests
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="Pubsub.html">
            
                <a href="Pubsub.html">
            
                    
                    Pubsub
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Code.html">
            
                <a href="Code.html">
            
                    
                    Code
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="CodeEngine.html">
            
                <a href="CodeEngine.html">
            
                    
                    CodeEngine
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="Dispatcher.html">
            
                <a href="Dispatcher.html">
            
                    
                    Dispatcher
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="Scheduler.html">
            
                <a href="Scheduler.html">
            
                    
                    Scheduler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="GFS.html">
            
                <a href="GFS.html">
            
                    
                    File system
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="Dashboard.html">
            
                <a href="Dashboard.html">
            
                    
                    Web Dashboard
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../Community/">
            
                <a href="../Community/">
            
                    
                    Community
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Community/involve.html">
            
                <a href="../Community/involve.html">
            
                    
                    Get involved
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Community/contribute.html">
            
                <a href="../Community/contribute.html">
            
                    
                    Contribute
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Community/feedback.html">
            
                <a href="../Community/feedback.html">
            
                    
                    Feedback
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Community/team.html">
            
                <a href="../Community/team.html">
            
                    
                    Team
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Developer API</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="api-documentation-for-developers">API Documentation for Developers</h1>
<p>ThingsJS is a framework for running JavaScript applications on IoT devices such as Raspberry PIs</p>
<ul>
<li>NOTE: This repository is currently under active development and its contents are subject to breaking changes.</li>
</ul>
<h1 id="directory-structure">Directory Structure</h1>
<pre><code>/bin
/docs
/lib
    /core
    /util
/util
    /dashboard
    /gfs
/samples
</code></pre><p>This repository is organized thus:</p>
<ol>
<li><a href="bin"><code>bin</code></a> contains the <code>things-js</code> CLI (shell script) that is installed with the package. Also contains default config files.</li>
<li><a href="docs"><code>docs</code></a> contains the API documentations generated by JSDoc.</li>
<li><a href="lib"><code>lib</code></a> contains the core ThingsJS code<ol>
<li><a href="lib/core"><code>core</code></a> contains the main ThingsJS objects such as <code>Code</code>, <code>CodeEngine</code>, <code>Pubsub</code>, <code>Dispatcher</code>.</li>
<li><a href="lib/util"><code>util</code></a> contains general-purpose utility modules used in the system.</li>
<li><a href="lib/extensions"><code>extensions</code></a> contains distributed file system for the workers.</li>
</ol>
</li>
<li><a href="util"><code>util</code></a> contains supplementary apps and debug tools<ol>
<li><a href="lib/dashboard"><code>dashboard</code></a> contains the ThingsJS Dashboard application; it is an <code>express</code> web-application</li>
<li><a href="lib/gfs"><code>gfs</code></a> contains the ThingsJS distributed filesystem. The file server can be run as a standalone <code>express</code> web-application or as a <code>express.Router</code> object to be plugged into an existing application.</li>
</ol>
</li>
<li><a href="samples"><code>samples</code></a> contains raw JavaScript sample code (non-instrumented) that can be dispatched to ThingsJS workers.</li>
</ol>
<h1 id="dependencies">Dependencies</h1>
<ul>
<li><p>The ThingsJS framework uses MQTT Pub/Sub as its main communication mechanism and assumes the existence of an active MQTT service. Either <strong>Mosquitto</strong>, or <strong>Mosca</strong> will do. ThingsJS provides a basic Mosca server that can be started with <code>things-js pubsub</code> command. The Pub/Sub service is referenced only by the URL (e.g. <code>mqtt://localhost</code>) within the ThingsJS framework and does not assume any specific implementation of the service.</p>
<pre><code>~$ things-js pubsub
</code></pre></li>
<li><p>For running the Dashboard web-application, <strong>MongoDB</strong> is required as the Dashboard uses it to store the end-user code.</p>
<pre><code>~$ service mongod start
</code></pre></li>
</ul>
<h1 id="getting-started">Getting Started</h1>
<p>For trying out the framework, you can follow the steps below:</p>
<h2 id="installation">Installation</h2>
<h3 id="option-1">Option 1</h3>
<ol>
<li><p><code>git clone</code> this repository</p>
<pre><code>~$ git clone https://github.com/karthikp-ubc/ThingsJS.git
</code></pre></li>
<li><p><code>npm install -g</code> to install the package. (download all the npm dependencies and link the CLI tool)
You may need to put <code>sudo</code> depending on your installation of NodeJS.
You need the <code>-g</code> (global installation) option for using the CLI. If you don&apos;t plan on using the CLI, you can omit the <code>-g</code> option.</p>
<pre><code>~$ cd ThingsJS
~/ThingsJS$ npm install -g
</code></pre></li>
</ol>
<h3 id="option-2">Option 2</h3>
<ol>
<li>Install via npm<pre><code>~$ sudo npm install -g things-js
</code></pre>You may omit the <code>sudo</code> depending on your NodeJS install settings.</li>
</ol>
<h3 id="using-the-cli">Using the CLI</h3>
<p>Along with the API provided, CLI is included for easy use.
Commands available currently:</p>
<ul>
<li>things-js dashboard</li>
<li>things-js worker {config}</li>
<li>things-js instrument {code}</li>
</ul>
<ol>
<li>To start the Dashboard Application:</li>
</ol>
<pre><code>~$ things-js dash

#OR

~$ things-js dashboard
</code></pre><p>By default it connects to MQTT at <code>localhost:1883</code>, MongoDB at <code>localhost:27017/things_dashboard</code>, and listens on <code>localhost:3000</code>.
To start the dashboard with a different configuration, you can use the <code>-c</code> or <code>--config</code> options with the config file path provided as an argument.
e.g.</p>
<pre><code>~$ things-js dash -c my_config.conf
</code></pre><p>This will start a web-application served at the specified port.
You can watch the demo of the Dashboard here:</p>
<p><a href="http://ece.ubc.ca/~kumseok/assets/ThingsJS_Migration.mp4" target="_blank"><img src="http://ece.ubc.ca/~kumseok/assets/ThingsJS_Migration.png" alt="Demo Screenshot" width="427" height="240" border="10"><p>Click Image to see Demo Video</p></a></p>
<ol>
<li>Running a ThingsJS worker:</li>
</ol>
<p>To start a ThingsJS worker, first you need to create a directory that will provide the NodeJS environment. This is because the worker needs to have a reference to the <code>things-js</code> module and any other npm modules that a ThingsJS user (developer) may require. If the worker cannot find a link to a <code>node_modules</code> directory, it will throw an error.</p>
<pre><code>~$ mkdir hello_things
~$ cd hello_things
~$ npm link things-js

#create a config file for the worker first (e.g. node_00.conf)

~/hello_things$ things-js worker node_00.conf
</code></pre><p>The configuration file is a required argument for starting the worker. It should contain the following information:</p>
<pre><code>{
    &quot;pubsub_url&quot;: &quot;mqtt://localhost&quot;,
    &quot;id&quot;: &quot;node_00&quot;,
    &quot;device&quot;: &quot;raspberry-pi3&quot;
}
</code></pre><ol>
<li>To instrument raw JavaScript code into a &quot;ThingJS-compatible&quot; code:</li>
</ol>
<pre><code>~$ things-js inst my_code.js

#OR

~$ things-js instrument my_code.js
</code></pre><p>By default the output file will have the same file name, with the extension <code>.things.js</code>.
To specify the output file name, provide the optional argument with <code>-o</code> or <code>--output</code>.
e.g.</p>
<pre><code>~$ things-js inst my_code.js -o my_code.instrumented.js
</code></pre><h2 id="license">License</h2>
<p>MIT</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../tutorials/dashboard/Dashboard.html" class="navigation navigation-prev " aria-label="Previous page: Using the Dashboard">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Devdocs.html" class="navigation navigation-next " aria-label="Next page: Developer Documentation">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Developer API","level":"1.4","depth":1,"next":{"title":"Developer Documentation","level":"1.4.1","depth":2,"path":"api/Devdocs.md","ref":"api/Devdocs.md","articles":[]},"previous":{"title":"Using the Dashboard","level":"1.3.3","depth":2,"path":"tutorials/dashboard/Dashboard.md","ref":"tutorials/dashboard/Dashboard.md","articles":[]},"dir":"ltr"},"config":{"plugins":["jsdoc"],"root":"./guide","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"jsdoc":{"options":"{}"},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"gitbook":"*"},"file":{"path":"api/README.md","mtime":"2018-10-15T23:34:02.479Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-04-24T23:23:12.622Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

